/**
 * 加密
 * https://www.npmjs.com/package/bcrypt
 */
const bcrypt = require('bcrypt')
const saltRounds = 10
// 测试代码
// const password = '123456'
// bcrypt.hash(password, saltRounds, function (err, passwordHash) {
//   // Store hash in your password DB.
//   bcrypt.compare(password, passwordHash, function (err, result) {
//     // result == true
//     console.log(result, 'result')
//   })
// })

// 加密
// encrypt('123456').then((passwordHash) => {
//   console.log(passwordHash, 'passwordHash')
// })
function encrypt(password) {
  return new Promise((resolve, reject) => {
    bcrypt.hash(password, saltRounds, function (err, passwordHash) {
      if (err) return reject(err)
      resolve(passwordHash)
    })
  })
}

// encryptCompare('123456').then((result) => {
//   console.log('result', result)
// })
// 验证
function encryptCompare(password, passwordHash) {
  console.log('dddddd', password, passwordHash)
  return new Promise((resolve, reject) => {
    bcrypt.compare('' + password, passwordHash, function (err, result) {
      if (err) return reject(err)
      resolve(result)
    })
  })
}

module.exports = {
  encrypt,
  encryptCompare
}
